<template>
  <div class="login_container">
    <el-row>
      <el-col :span="12" :xs="0"></el-col>
      <el-col :span="12" :xs="24">
        <!-- 登录的表单 -->
        <el-form
          class="login_form"
          :model="loginForm"
          :rules="rules"
          ref="ruleFormRef"
        >
          <h1>Hello</h1>
          <h2>欢迎来到老王甄选</h2>
          <el-form-item prop="username">
            <el-input
              :prefix-icon="User"
              v-model="loginForm.username"
            ></el-input>
          </el-form-item>
          <el-form-item prop="password">
            <el-input
              :prefix-icon="Lock"
              type="password"
              v-model="loginForm.password"
              show-password
            ></el-input>
          </el-form-item>
          <el-form-item>
            <el-button
              class="login_btn"
              :loading="loading"
              type="primary"
              size="default"
              @click="login"
              >登录</el-button
            >
          </el-form-item>
        </el-form>
      </el-col>
    </el-row>
  </div>
</template>

<script setup lang="ts">
import { User, Lock } from "@element-plus/icons-vue";
import { ElNotification } from "element-plus";
import { reactive, ref } from "vue";
import useUserStore from "@/store/modules/user";
import { getTime } from "@/utils/time";
import { useRouter,useRoute } from "vue-router";
let $Router = useRouter();
let $route = useRoute()
let useStore = useUserStore();
// 收集表单数据
let loginForm = reactive({ username: "admin", password: "111111" });
let loading = ref(false);
const ruleFormRef = ref();
// 登录
const login = async () => {
    // 保证所有表单校验通过再请求
  await ruleFormRef.value.validate();
  loading.value = true;
  try {
    await useStore.userLogin(loginForm);
    ElNotification({
      type: "success",
      message: getTime() + "好",
    });
    loading.value = false;
     //判断登录的时候,路由路径当中是否有query参数，如果有就往query参数挑战，没有跳转到首页
    let redirect: any = $route.query.redirect
    $Router.push({path:redirect || '/'});
  } catch (error) {
    ElNotification({
      type: "error",
      message: (error as Error).message,
    });
    loading.value = false;
  }
};
// 自定义校验规则
const validateUserName = (rule: any, value: any, callback: any) => {
  if (value.length >= 5) {
    callback();
  } else {
    callback(new Error("账号不能低于5位"));
  }
};
const validatePwd = (rule: any, value: any, callback: any) => {
  if (value.length >= 6) {
    callback();
  } else {
    callback(new Error("密码长度至少六位"));
  }
};
const rules = reactive({
  username: [{ validator: validateUserName }],
  password: [{ validator: validatePwd }],
});
</script>

<style scoped lang="scss">
.login_container {
  width: 100%;
  height: 100vh;
  background: url("@/assets/images/background.jpg") no-repeat;
  background-size: cover;

  .login_form {
    position: relative;
    width: 80%;
    top: 30vh;
    background: url("@/assets/images/login_form.png") no-repeat;
    background-size: cover;
    padding: 40px;

    h1 {
      color: white;
      font-size: 40px;
    }

    h2 {
      font-size: 20px;
      color: white;
      margin: 20px 0px;
    }

    .login_btn {
      width: 100%;
    }
  }
}
</style>
